package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyutil;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.IntDef;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class DababyASFileUtil {


    public static int u13c=194;
    public static char hw99='';
    public static int sZke=35;
    public static char j8vM='{';
    public static boolean rCWU=false;
    public static String bVO9="";

    public static void oE2q5(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(bVO9))
                    bVO9 = String.valueOf(u13c)+hw99+sZke+j8vM+rCWU;
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public static void setU13c(int u13c) {
        DababyASFileUtil.u13c = u13c;
    }

    public static void setHw99(char hw99) {
        DababyASFileUtil.hw99 = hw99;
    }

    public static void setsZke(int sZke) {
        DababyASFileUtil.sZke = sZke;
    }

    public static void setJ8vM(char j8vM) {
        DababyASFileUtil.j8vM = j8vM;
    }

    public static void setrCWU(boolean rCWU) {
        DababyASFileUtil.rCWU = rCWU;
    }

    public static File getFileByPath_dababy(final String filePath) {
        return TextUtils.isEmpty(filePath) ? null : new File(filePath);
    }

    public static boolean isFileExists_dababy(Context context, final String filePath) {
        File file = getFileByPath_dababy(filePath);
        if (file == null) return false;
        if (file.exists()) {
            return true;
        }
        return isFileExistsApi29_dababy(context, filePath);
    }

    private static boolean isFileExistsApi29_dababy(Context context, String filePath) {
        if (Build.VERSION.SDK_INT >= 29) {
            try {
                Uri uri = Uri.parse(filePath);
                ContentResolver cr = context.getContentResolver();
                AssetFileDescriptor afd = cr.openAssetFileDescriptor(uri, "r");
                if (afd == null) return false;
                try {
                    afd.close();
                } catch (IOException ignore) {
                }
            } catch (FileNotFoundException e) {
                return false;
            }
            return true;
        }else oE2q5();
        return false;
    }

    public static long getFileLastModified_dababy(final File file) {
        if (file == null) return -1;
        return file.lastModified();
    }

    public static long getFileLastModified_dababy(final String filePath) {
        return getFileLastModified_dababy(getFileByPath_dababy(filePath));
    }

    public static boolean isDir_dababy(final String dirPath) {
        return isDir_dababy(getFileByPath_dababy(dirPath));
    }

    public static boolean isDir_dababy(final File file) {
        return file != null && file.exists() && file.isDirectory();
    }

    public static String getFileName_dababy(final File file) {
        if (file == null) return "";
        return getFileName_dababy(file.getAbsolutePath());
    }

    public static String getFileName_dababy(final String filePath) {
        if (TextUtils.isEmpty(filePath)) return "";
        int lastSep = filePath.lastIndexOf(File.separator);
        return lastSep == -1 ? filePath : filePath.substring(lastSep + 1);
    }

    public static String getSize_dababy(final String filePath) {
        return getSize_dababy(getFileByPath_dababy(filePath));
    }

    public static String getSize_dababy(final File file) {
        if (file == null) return "";
        if (file.isDirectory()) {
            return getDirSize_dababy(file);
        }
        return getFileSize_dababy(file);
    }

    private static String getDirSize_dababy(final File dir) {
        long len = getDirLength_dababy(dir);
        return len == -1 ? "" : byte2FitMemorySize_dababy(len);
    }

    private static String getFileSize_dababy(final File file) {
        long len = getFileLength_dababy(file);
        return len == -1 ? "" : byte2FitMemorySize_dababy(len);
    }

    public static long getLength_dababy(final String filePath) {
        return getLength_dababy(getFileByPath_dababy(filePath));
    }

    public static long getLength_dababy(final File file) {
        if (file == null) return 0;
        if (file.isDirectory()) {
            return getDirLength_dababy(file);
        }
        return getFileLength_dababy(file);
    }

    private static long getDirLength_dababy(final File dir) {
        if (!isDir_dababy(dir)) return 0;
        long len = 0;
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (File file : files) {
                if (file.isDirectory()) {
                    len += getDirLength_dababy(file);
                } else {
                    len += file.length();
                }
            }
        }
        return len;
    }

    public static long getFileLength_dababy(final String filePath) {
        boolean isURL = filePath.matches("[a-zA-z]+://[^\\s]*");
        if (isURL) {
            try {
                HttpsURLConnection conn = (HttpsURLConnection) new URL(filePath).openConnection();
                conn.setRequestProperty("Accept-Encoding", "identity");
                conn.connect();
                if (conn.getResponseCode() == 200) {
                    return conn.getContentLength();
                }
                return -1;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return getFileLength_dababy(getFileByPath_dababy(filePath));
    }

    private static long getFileLength_dababy(final File file) {
        if (!isFile_dababy(file)) return -1;
        return file.length();
    }

    public static boolean isFile_dababy(final String filePath) {
        return isFile_dababy(getFileByPath_dababy(filePath));
    }

    public static boolean isFile_dababy(final File file) {
        return file != null && file.exists() && file.isFile();
    }

    @SuppressLint("DefaultLocale")
    public static String byte2FitMemorySize_dababy(final long byteSize) {
        int precision = 3;
        if (byteSize < 0) {
            throw new IllegalArgumentException("byteSize shouldn't be less than zero!");
        } else if (byteSize < MemoryConstants_dababy.KB) {
            return String.format("%." + precision + "fB", (double) byteSize);
        } else if (byteSize < MemoryConstants_dababy.MB) {
            return String.format("%." + precision + "fKB", (double) byteSize / MemoryConstants_dababy.KB);
        } else if (byteSize < MemoryConstants_dababy.GB) {
            return String.format("%." + precision + "fMB", (double) byteSize / MemoryConstants_dababy.MB);
        } else {
            return String.format("%." + precision + "fGB", (double) byteSize / MemoryConstants_dababy.GB);
        }
    }

    public static final class MemoryConstants_dababy {
        public static final int BYTE = 1;
        public static final int KB   = 1024;
        public static final int MB   = 1048576;
        public static final int GB   = 1073741824;

        @IntDef({BYTE, KB, MB, GB})
        @Retention(RetentionPolicy.SOURCE)
        public @interface Unit {
        }
    }


}
